package com.li.server.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

public class JDBCUtil {
	public <T> T executeSQL(JDBCExecutor<T> executor) {
		Connection currentConn = ConnectionUtil.getCurrentConnection();
		T result;
		try {
			currentConn.setAutoCommit(Boolean.FALSE);
			
			result = executor.execute(currentConn);
			
			currentConn.commit();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			
			try {
				currentConn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			
			throw new RuntimeException(e);
		} 
	}
}
